<template>
	<view class="page">
		<u--form labelPosition="top" :model="model" ref="form" :rules="rules" labelWidth="auto" :labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}">
			<view class="basic-style">
				<view class="basic-title basic-border">基本信息</view>
				<u-form-item label="入职人员" prop="applyId" required>
					<h-select-register-input v-model="model.applyId" class="w-100" placeholder="请选择入职人员"
						searchPlaceholder="请输入" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"></h-select-register-input>
				</u-form-item>
				<u-form-item label="入职部门" prop="departmentId" :borderBottom="false" required>
					<h-select-depart-input v-model="model.departmentId" :departName="model.departmentId_dictText"
						placeholder="请选择入职部门" border="surround" style="width: 100%" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:title.sync="model.departmentId_dictText"></h-select-depart-input>
				</u-form-item>
				<u-form-item label="岗位序列" prop="positionCategory" required>
				  <h-select-input-tag
				    class="w-100"
				    v-model="model.positionCategory"
				    objectField="model.positionCategory"
				    placeholder="请选择岗位序列"
				    border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
				    dictName="position_category"
				  ></h-select-input-tag>
				</u-form-item>
				<u-form-item label="职务级别" prop="positionLevelId" required>
				  <h-select-input-tag
				    class="w-100"
				    v-model="model.positionLevelId"
				    objectField="model.positionLevelId"
				    placeholder="请选择职务级别"
				    border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
				    :dictName="positionLevelIdDict"
				  ></h-select-input-tag>
				</u-form-item>
				<u-form-item label="入职岗位" prop="positionId" :borderBottom="false" ref="item1" required>
					<h-dict-select style="width: 100%" v-model="model.positionId" placeholder="请选择入职岗位"
						:dictName="positionIdDict" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-dict-select>
				</u-form-item>
				<u-form-item label="入职类型" prop="inductionType" :borderBottom="false" ref="item1" required>
					<h-dict-select style="width: 100%" v-model="model.inductionType" placeholder="请选择入职类型"
						dictName="induction_type" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-dict-select>
				</u-form-item >
				<u-form-item label="推荐人" v-if="model.inductionType=='4'" prop="recommendId" :borderBottom="false" ref="item1">
					<!-- <h-dict-select style="width: 100%" v-model="model.recommendId" placeholder="请选择推荐人"
						dictName="induction_type" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-dict-select> -->
						<h-select-person-input
						  style="width: 100%"
						  v-model="model.recommendId"
							objectField="model.recommendId"
						  placeholder="请选择推荐人"
						  border="surround"
						  suffixIcon="arrow-down-fill"
						  :suffixIconStyle="{ color: '#B2B2B2' }"
						></h-select-person-input>
				</u-form-item>
				<u-form-item label="所属团队" prop="teamId" required>
					<h-select-team-input style="width: 100%" v-model="model.teamId" objectField="model.teamId"
						placeholder="请选择所属团队" border="surround" :tdlx="3" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-select-team-input>
				</u-form-item>
				<u-form-item label="试用期" prop="probationership" :borderBottom="false" ref="item1" required>
					<h-dict-select style="width: 100%" v-model="model.probationership" placeholder="请选择试用期"
						dictName="probationership" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-dict-select>
				</u-form-item>
				<u-form-item label="转正后薪资(元)" prop="conversionSalary" :borderBottom="false" ref="item1" required>
					<h-money-input placeholder="请输入转正后薪资" v-model="model.conversionSalary"
						@input="conversionSalaryChange"></h-money-input>
				</u-form-item>
				<u-form-item label="试用期薪资(元)" prop="trialSalary" :borderBottom="false" ref="item1">
					<h-money-input placeholder="请输入试用期薪资" v-model="model.trialSalary"></h-money-input>
				</u-form-item>
				<u-form-item label="参保单位" prop="insuredUnit" :borderBottom="false" required>
					<h-select-company-input style="width: 100%;" v-model="model.insuredUnit"
						objectField="model.insuredUnit" placeholder="请选择参保单位" border="surround"
						suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}"></h-select-company-input>
				</u-form-item>
				<u-form-item label="参保基数(元)" prop="insuredBase" :borderBottom="false" ref="item1" required>
					<!-- <h-money-input placeholder="请输入参保基数" v-model="model.insuredBase"></h-money-input> -->
					<u--input
						type="number"
						placeholder="请输入参保基数" 
						v-model="model.insuredBase"
					></u--input>
				</u-form-item>
				<u-form-item label="工作性质" prop="workType" :borderBottom="false" ref="item1" required>
					<h-dict-select style="width: 100%" v-model="model.workType" placeholder="请选择工作性质"
						dictName="work_type" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-dict-select>
				</u-form-item>
				
				<u-form-item label="到岗时间" prop="arrivalTime" :borderBottom="false" required>
					<h-Calendar-select style="width: 100%" border="surround" v-model="model.arrivalTime"
						suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}" calendarMode="single"
						placeholder="请选择到岗时间" @confirm="confirmArrivalTime"></h-Calendar-select>
				</u-form-item>
				<u-form-item label="执业金(元)" :borderBottom="false" prop="practicingGold">
					<!-- <u--input
						type="number"
						placeholder="请输入执业金" 
						v-model="model.practicingGold"
					></u--input> -->
					<h-money-input placeholder="请输入执业金" v-model="model.practicingGold"></h-money-input>
				</u-form-item>
				<u-form-item label="备注" prop="remark" :borderBottom="false" ref="item1">
					<u--textarea v-model="model.remark" :confirmType="null" count placeholder="请输入备注"
						:maxlength="500"></u--textarea>
				</u-form-item>
			</view>
			<view class="basic-style">
				<view class="basic-title basic-border">附件信息</view>
				<view class="py-1 space-y-1" style="width: 100%">
					<view style="color: red">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<h-upload-file v-model="model.attachments" :isIcon="false" />
					<file-list v-if="model.attachments" v-model="model.attachments" :isDelete="true" />
				</view>
			</view>
			<view class="space-x-1 px-1 btn_position">
				<u-button :disabled="isSubmit" type="primary" :plain="true" text="保存" @click="onDraft"></u-button>
				<u-button :disabled="isSubmit"  type="primary"  text="保存并提交" @click="onSubmit"></u-button>
			</view>
			<view style="height: 100rpx;"></view>
		</u--form>
		<u-toast ref="uToast"></u-toast>
		<ProcessNodeConfiguration ref="processConfig" @ok="onOk"></ProcessNodeConfiguration>
	</view>
</template>

<script>
	import fileList from '@/pages/synergy/components/fileList.vue'
	import moment from 'moment'
	import {
		generatedFormNumber, // 获取单据编号
		addEntryPerson, // 入职申请-新增保存
		queryByIdEntryPerson,
		editEntryPerson,
		queryEntryPersonInfo
	} from '@/api/index.js'
	import ProcessNodeConfiguration from '@/components/ProcessNodeConfiguration/ProcessNodeConfiguration.vue' // 流程节点配置人员选择弹窗
	export default {
		components: {
			fileList,
			ProcessNodeConfiguration
		},
		data() {
			return {
				id: '',
				positionIdDict: 'sys_position,name,id,del_flag = 0 and audit_status=2 and is_virtual=0',
				positionLevelIdDict: 'erp_org_position_level,level_name,id,del_flag="0" and audit_status="2"',
				model: {
					applyId: '', //入职人员id
					departmentId: '', //入职部门id
					departmentId_dictText: '', //入职部门描述
					positionCategory: '', // 岗位序列
					positionLevelId: '', // 职务级别
					positionId: '', //入职岗位
					inductionType: '', //入职类型
					recommendId: '', // 推荐人
					workType: '', // 工作性质
					teamId: '', //所属团队
					probationership: '', //试用期
					conversionSalary: '', //转正后薪资
					trialSalary: '', //试用期薪资
					insuredUnit: '', //参保单位
					insuredBase: '', //參保基數
					practicingGold: '', //执业金
					arrivalTime: '', //到岗时间
					remark: '', //备注
					attachments: '', //上传附件
				},
				rules: {
					applyId: [{
						required: true,
						message: '请选择入职人员!',
						trigger: 'change'
					}],
					departmentId: [{
						required: true,
						message: '请选择入职部门!',
						trigger: 'change'
					}],
					positionId: [{
						required: true,
						message: '请选择入职岗位!',
						trigger: 'change'
					}],
					positionCategory: [{
						required: true,
						message: '请选择岗位序列!',
						trigger: 'change'
					}],
					positionLevelId: [{
						required: true,
						message: '请选择职务级别!',
						trigger: 'change'
					}],
					inductionType: [{
						required: true,
						message: '请选择入职类型!',
						trigger: 'change'
					}],
					workType: [{
						required: true,
						message: '请选择工作性质!',
						trigger: 'change'
					}],
					teamId: [{
						required: true,
						message: '请选择所属团队!',
						trigger: 'change'
					}],
					probationership: [{
						required: true,
						message: '请选择试用期!',
						trigger: 'change'
					}],
					conversionSalary: [{
						required: true,
						message: '请输入转正后薪资!',
						trigger: 'change'
					}],
					insuredUnit: [{
						required: true,
						message: '请选择参保单位!',
						trigger: 'change'
					}],
					insuredBase: [{
						required: true,
						message: '请输入参保基数!',
						trigger: 'change'
					}],
					arrivalTime: [{
						required: true,
						message: '请选择到岗时间!',
						trigger: 'change'
					}],
				},
				loading: false,
				isSubmit: false,
				//代码生成后需手动设置流程编码
				flowCode: 'onl_erp_hr_induction_bill',
				formUrl: "erp/hr/induction/modules/ErpHrInductionBillForm",
				formUrlMobile: "/pages/synergy/erpHrInductionBillList",
				processId:"1540230524692357122",
				beanName:"erpHrInductionBillServiceImpl",
			}
		},
		watch: {
			// 根据板块类别筛选文件类别下拉数据
			'model.plateType'(nv, ov) {
				this.fileTypeTitle = ''
				this.model.fileType = ''
				const ary = this.fileTypeOptions.filter(i => i.bindPlateType.includes(Number(nv)))
				this.fileTypeColumns = [ary]
			},
			'model.applyId'(nv, ov) {
				queryEntryPersonInfo({id:nv}).then(res => {
					if(res.success){
						this.model.bpmBizTitle = res.result.name || ''
					}
				})
			},
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id
				this.edit(options.id)
			}
		},
		methods: {
			async edit(id){
				const response = await queryByIdEntryPerson({ id })
				const data = response?.result || {}
				this.model = await Object.assign({}, data)
			},
			// 获取流程列表
			getProcessList (id) {
				if (this.isSubmit) {
					this.$refs.processConfig.showPopup = true
					this.$refs.processConfig.formUrl = this.formUrl
					this.$refs.processConfig.formUrlMobile = this.formUrlMobile
					this.$refs.processConfig.flowCode = this.flowCode
					this.$refs.processConfig.edit(id, this.processId, this.beanName)
				}
			},
			// 提交流程成功
			onOk () {
				this.$refs.uToast.show({
					message: '提交成功',
					type: 'success',
					complete () {
						if (getCurrentPages().length > 1) {
							uni.navigateBack()
						} else {
							uni.reLaunch({
								url: '/pages/home/home'
							})
						}
						
					}
				})
			},
			// 确认选择文件类别
			fileTypeConfirm(e) {
				this.fileTypeTitle = e.value[0].text
				this.model.fileType = e.value[0].id
				this.fileTypeShow = false
			},
			// 试用期薪资默认转正后薪资的80%
			conversionSalaryChange(value) {
				const num = value * 10000 * 0.8 / 10000
				this.model.trialSalary = num.toFixed(2)
			},
			confirmArrivalTime(e) {
				this.model.arrivalTime = e[0]
			},
			// 保存提交
			async onSubmit() {
				const val = await this.$refs.form.validate()
				if (!val) return
				this.isSubmit = true
				try {
				  this.loading = true
					var url = ''
					if (this.id) {
						url = editEntryPerson
					} else {
						url = addEntryPerson
					}
				  const response = await url(this.model)
				  this.$u.toast(response.message)
				  if (response.success) {
				    var that = this
				    
				    // 获取流程列表
				    if (this.model.id) {
				    	this.getProcessList(this.model.id)
				    } else {
				    // 判断添加的时候是否有返回id值
				    	if (response.result) {
				    		this.getProcessList(response.result)
				    	}
				    
				    }
				    this.$refs.uToast.show({
				    	message: response.message,
				    	type: 'default',
				    	complete () {
				    		// that.isSubmit = false
				    	}
				    })
				  }
				} finally {
				  this.loading = false
				}
			},
			// 保存
			async onDraft() {
				// const val = await this.$refs.form.validate()
				// if (!val) return
				this.isSubmit = true
				try {
				  this.loading = true
					var url = ''
					if (this.id) {
						url = editEntryPerson
					} else {
						url = addEntryPerson
					}
				  const response = await url(this.model)
				  if (response.success) {
						this.$refs.uToast.show({
							message: response.message,
							type: 'default',
							complete () {
								this.isSubmit = false
								if (getCurrentPages().length > 1) {
									uni.navigateBack()
								} else {
									uni.reLaunch({
										url: '/pages/home/home'
									})
								}
								
							}
						})
				  }
				} finally {
				  this.loading = false
				}
			}
		}
	}
</script>


<style lang="scss" scoped>
	@import url("@/assets/inputStyle.scss");
</style>